<template>
  <section>
    <el-row>
      <el-col class="header" :span="21">
        <div class="title">
          <span>用户管理</span>
        </div>
      </el-col>
      <el-col :span="21">
        <!--搜索与添加-->
        <el-row class="flex" :gutter="10">
          <el-col :span="12">
            <el-input
              placeholder="搜索用户"
              v-model="queryInfo.query"
              :clearable="true"
              @input="changeInput"
            >
              <el-button
                slot="append"
                @click="searchUser"
                icon="el-icon-search"
              ></el-button>
            </el-input>
          </el-col>
          <el-col :span="6">
            <el-button type="primary" @click="addDialogVisible = true"
              >添加用户
            </el-button>
          </el-col>
        </el-row>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="21">
        <!--用户列表-->
        <el-table :data="userList" border stripe>
          <el-table-column
            label="序号"
            type="index"
            width="60"
          ></el-table-column>
          <el-table-column
            label="姓名"
            prop="username"
            width="300"
          ></el-table-column>
          <el-table-column
            label="用户注册日期"
            prop="createdAt"
          ></el-table-column>
          <el-table-column label="操作" width="200">
            <template v-slot="scope">
              <!--删除按钮-->
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="removeUserById(scope.row.id)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>

    <!--添加用户的对话框-->
    <el-dialog
      title="添加用户"
      width="50%"
      :visible.sync="addDialogVisible"
      :close-on-click-modal="false"
      @close="addDialogClosed"
    >
      <!--内容主体-->
      <el-form :model="addForm" :rules="addFormRules" label-width="70px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>
      </el-form>
      <!--底部-->
      <span slot="footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>
  </section>
</template>
<script>
// 引入请求模块
import { $http } from "../../request/service";

export default {
  name: "Data",
  data() {
    return {
      //获取用户列表的参数对象
      queryInfo: {
        query: "",
      },
      userList: [],
      addDialogVisible: false,
      //添加用户的表单数据
      addForm: {
        username: "",
        password: "",
      },
      //添加用户表单的验证规则对象
      addFormRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 5,
            max: 10,
            message: "用户名为英文，长度在5~10个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 15, message: "密码长度在6~15个字符", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getUserList();
  },
  methods: {
    // 输入框改变事件
    changeInput() {
      if (this.queryInfo.query == "") return this.getUserList();
    },
    // 搜索用户
    searchUser() {
      $http.get(`/searchUser?name=${this.queryInfo.query}`).then((res) => {
        if (res.code !== 200) return this.$message.error("获取失败");
        res.data.filter((item) => {
          item.createdAt = item.createdAt.split("T", 1);
        });
        this.userList = res.data;
      });
    },
    //get所有注册过的用户
    getUserList() {
      $http.get(`/userList`).then((res) => {
        if (res.code !== 200) return this.$message.error(res.data.msg);
        res.data.filter((item) => {
          item.createdAt = item.createdAt.split("T", 1);
        });
        this.userList = res.data;
      });
    },
    addDialogClosed() {
      this.addForm.username = "";
      this.addForm.password = "";
    },
    //添加用户
    addUser() {
      $http.post(`/register`, this.addForm).then((res) => {
        if (res.data.msg !== "用户注册成功")
          return this.$message.error(res.data.msg);
        this.addDialogVisible = false;

        this.$message({
          message: "用户添加成功",
          type: "success",
        });
        this.getUserList();
      });
    },
    //删除用户
    removeUserById(id) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          $http.post(`deleteUser`, { id }).then((res) => {
            if (res.data.msg !== "删除成功")
              return this.$message.error("删除失败");
            this.$message.success("删除成功");
            this.getUserList();
          });
        })
        .catch(() => {
          this.$message.info("已取消删除");
        });
    },
  },
};
</script>
<style scoped>
.header {
  display: flex;
  align-content: center;
  justify-content: space-between;
}

.el-row {
  padding-top: 50px;
  display: flex;
  align-items: center;
}
.title {
  display: flex;
  align-items: center;
  text-align: start;
  font-size: 20px;
  font-weight: 700;
  height: 50px;
}
.searchWai {
  padding-left: 300px;
}
</style>
